﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ApplicationLibrary.SqlConnector;
using ApplicationLibrary;
using Clothes.Items.ModuleData;

namespace Clothes.Sales.Forms
{
    public partial class ReturnToCustomerFilter : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        public List<int> ItemsID = new List<int>();

        int idCustomer;

        public ReturnToCustomerFilter(int idCust = -1)
        {
            idCustomer = idCust;
            InitializeComponent();
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            LoadData();
        }

        private void LoadData()
        {
            QueryBuilder qb = new QueryBuilder();
            using (SqlABCommand scc = new SqlABCommand(GlobalInfo.LoginInfo.ProviderType))
            {
                scc.Connection = GlobalInfo.SqlConnection;

                qb.AddSelect();
                qb.AddFrom(CL_Items.Name);

                SqlABParameter sqlP1 = new SqlABParameter("@p1", CL_Items.ReturnToCustomer);
                qb.AddCompare(CL_Items.ReturnToCustomer, sqlP1);
                sqlP1.Value = false;

                SqlABParameter sqlP2 = new SqlABParameter("@p2", CL_Items.Invoice);
                qb.AddCompare(CL_Items.Invoice, sqlP2);
                sqlP2.Value = false;

                SqlABParameter sqlP4 = new SqlABParameter("@p4", CL_Items.Customer);
                qb.AddCompare(CL_Items.Customer, sqlP4);
                sqlP4.Value = idCustomer;

                SqlABParameter sqlP5 = new SqlABParameter("@p5", CL_Items.Status);
                qb.AddCompare(CL_Items.Status, sqlP5);
                sqlP5.Value = ItemStatus.E_Load.Int();

                SqlABParameter sqlP6 = new SqlABParameter("@p6", CL_Items.DeadLineDate);
                qb.AddCompare(CL_Items.DeadLineDate, sqlP6, "<=");
                sqlP6.Value = GlobalInfo.CurrentDate;

                scc.Parameters.AddRange(new List<SqlABParameter> { sqlP1, sqlP2, sqlP4, sqlP5, sqlP6});

                scc.CommandText = qb.Query;

                SqlABDataReader dr = scc.ExecuteReader();
                if (dr.HasRows)
                    lsvItems.Items.Clear();
                try
                {
                    while (dr.Read())
                    {
                        if (!ItemsID.Contains(dr.GetValue<int>(CL_Items.ID)))
                            LoadRow(dr);
                    }
                }
                catch (System.Exception e)
                {
                    if (ApplicationLibrary.Emailer.EmailSender.SendException)
                    {
                        ApplicationLibrary.Emailer.EmailSender.ToAddress = "mauro.rogledi@gmail.com";
                        ApplicationLibrary.Emailer.EmailSender.SendMail(e.Message, e.StackTrace, "");
                    }
                }
                finally
                {
                    dr.Close();
                }
            }
        }

        private void LoadRow(SqlABDataReader dr)
        {
            ItemsID.Add(dr.GetValue<int>(CL_Items.ID));

            ListViewItem lvi = new ListViewItem();
            lvi.Text = dr.GetValue<int>(CL_Items.ID).ToString();
            lvi.SubItems.Add(dr.GetValue<string>(CL_Items.Description)).Name = CL_Items.Description.Name;
            lvi.SubItems.Add(dr.GetValue<double>(CL_Items.Price).ToString("c")).Name = CL_Items.Price.Name;
            lvi.SubItems[CL_Items.Price.Name].Tag = dr.GetValue<double>(CL_Items.Price).ToString();
            lvi.SubItems.Add(dr.GetValue<DateTime>(CL_Items.ReceivingData).ToShortDateString()).Name = CL_Items.ReceivingData.Name;
            lvi.SubItems.Add(dr.GetValue<DateTime>(CL_Items.DeadLineDate).ToShortDateString()).Name = CL_Items.DeadLineDate.Name;
            
            lsvItems.Items.Add(lvi);
        }
    }
}
